package org.ilovecode.data {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import org.ilovecode.utils.Utilities;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class GalleryRepository extends EventDispatcher {
		
		private var _items:Array;
		private var _numLoaded:int;
		private var _specialLoadingStructure:String = "none";
		
		public function GalleryRepository(url:String, specialLoadingStructure:String = "none") {
			_specialLoadingStructure = specialLoadingStructure;
			_items = new Array();
			_numLoaded = 0;
			
			Utilities.loadURL(url, xml_Complete);
		}
		
		//{ region Handlers
		
		private function xml_Complete(event:Event):void {
			var xml:XML = XML(event.target.data);
			
			if (xml.children().length() > 0) {
				for each(var item:XML in xml.children()) {
					var gi:GalleryItem;
					switch (_specialLoadingStructure) {
						case "none":
							if (("name" in item) && ("image" in item) && ("thumb" in item)) {
								gi = new GalleryItem(item.name, item.image, item.thumb);
								gi.addEventListener(Event.COMPLETE, galleryItem_Complete);
								_items.push(gi);
							} 
							break;
						case "layout":
							if (("code" in item) && ("layout" in item) && ("layoutthumb" in item)) {
								gi = new GalleryItem(item.code, "http://" + item.layout, "http://" + item.layoutthumb);
								gi.addEventListener(Event.COMPLETE, galleryItem_Complete);
								_items.push(gi);
							} 
							break;
						case "topview":
							if (("code" in item) && ("topview" in item) && ("topviewthumb" in item)) {
								gi = new GalleryItem(item.code, "http://" + item.topview, "http://" + item.topviewthumb, true);
								gi.addEventListener(Event.COMPLETE, galleryItem_Complete);
								_items.push(gi);
							}
							break;
					}
				}
			} else {
				throw new Error("Gallery images not found");
			}
		}
		
		private function galleryItem_Complete(event:Event):void {
			_numLoaded++;
			
			if (numLoaded == numItems) {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		//} endregion
		
		public function getAllThumbnails():Array {
			var thumbs:Array = new Array();
			for each (var item:GalleryItem in _items) {
				thumbs.push(item.thumb);
			}
			
			return thumbs;
		}
		
		public function getImageByName(name:String):Sprite {
			for each (var item:GalleryItem in _items) {
				if (item.name == name) {
					return item.image;
				}
			}
			
			return null;
		}
		
		public function getImageByIndex(index:int):Sprite {
			if (index >= 0 && index < _items.length) {
				return GalleryItem(_items[index]).image;
			}
			
			return null;
		}
		
		/*private function getItemByName(name:String):GalleryItem {
			for each (var item:GalleryItem in _items) {
				if (item.name == name) {
					return item;
				}
			}
			
			return null;
		}
		
		private function getItemByIndex(index:int):GalleryItem {
			if (index >= 0 && index < _items.length) {
				return _items[index];
			}
			
			return null;
		}*/
		
		//{ region Getters
		
		public function get numLoaded():int {
			return _numLoaded;
		}
		
		public function get numItems():int {
			return _items.length;
		}
		
		public function get percentLoaded():int {
			return Math.round((numLoaded / numItems) * 100);
		}
		
		//} endregion
	}

}